package com.meixun.xuanke.user.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.meixun.xuanke.user.model.User;

import javax.servlet.http.HttpServletRequest;


public class RequestUtil {

    /**
     * 学校id
     * @param request
     * @return
     */
    public static String getSchoolId(HttpServletRequest request) {
        User user=get(request);
        return user==null?null:user.getSchoolId();
    }


    /**
     * 用户user
     * @param request
     * @return
     */
    public static User getUser(HttpServletRequest request) {
        return get(request);
    }

    /**
     * 用户userId
     * @param request
     * @return
     */
    public static String getUserId(HttpServletRequest request) {
        User user=get(request);
        return user==null?null:user.getUserId();
    }


    /**
     * 学生userid
     * @param request
     * @return
     */
    public static String getStuUserId(HttpServletRequest request) {
        User user=get(request);
        return user==null?null:user.getStuUserId();
    }

    /**
     * 年级
     * @param request
     * @return
     */
    public static Integer getGrade(HttpServletRequest request) {
        User user=get(request);
        return user==null?null:user.getGrade();
    }


    public static User get(HttpServletRequest request) {
        String userJson= (String) request.getAttribute("userJson");
        if(userJson!=null){
            return  JSON.parseObject(userJson, new TypeReference<User>() {});
        }
        return null;
    }

}
